package list;

import java.util.Arrays;

public class MyArrayList {
    public int[] elem;
    public  int usedsize;
    //private static final  int DEFAULT_SIZE=8;

    public MyArrayList() {
        this.elem=new int[5];//int[DEFAULT_SIZE];
    }
    public void display(){
        for (int i = 0; i < usedsize; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println(" ");
    }
    public void add(int data){
        if(isFull()){
            this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[usedsize]=data;
        this.usedsize++;
    }
    public  boolean isFull(){
        if(this.usedsize==this.elem.length){
            return true;
        }
        return false;
    }
    public boolean cheak(int pos){
        if(pos<0||pos>this.usedsize){
            System.out.println("error");
            return  false;
        }
        return  true;
    }
    public void add(int pos,int data){
        if(!cheak(pos)){
            throw  new MyArrayListIndexOutOfExpection("unlegal");
        }
        if(isFull()){
            this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
        }
        for (int i = this.usedsize-1; i >=pos ; i--) {
            this.elem[i+1]=this.elem[i];
        }
        this.elem[pos]=data;
        this.usedsize++;
    }
    public boolean find(int toFind){
        for (int i = 0; i < this.elem.length; i++) {
            if(this.elem[i]==toFind){
                return true;
            }
        }
        return false;
    }
    public  int findout(int toFind){
        for (int i = 0; i < this.elem.length; i++) {
            if(this.elem[i]==toFind){
                return i;
            }
        }
        return -1;
    }
    public int get(int pos){
        if(pos<0||pos>=this.usedsize){
            System.out.println("error");
            return -1;
        }
        else if(this.usedsize==0){
            return -999;
        }
          else {
              return this.elem[pos];
        }
    }
    public void set(int pos ,int value){
        if(!cheak(pos)){
            throw  new MyArrayListIndexOutOfExpection("unlegal");
        }
        this.elem[pos]=value;
    }
    public void remove(int pos){
        if(pos<0||pos>=this.usedsize){
            System.out.println("error");
            return;
        }
        for (int i = pos; i <this.elem.length-1 ; i++) {
            this.elem[i]=this.elem[i+1];
        }
        this.usedsize--;

    }
    public  int size(){
        return this.usedsize;
    }
    public void clear(){
        this.usedsize=0;
    }
}
